package com.controller.user;

import com.entity.GoodsReceiveInfo;
import com.service.user.GRIService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Good Receive Controller
 *
 * <p>响应收货信息页的请求
 */
@RestController
@CrossOrigin
public class GRIController {

  @Autowired GRIService griService;

  /**
   * 获取指定用户的收货信息列表
   *
   * @param userId 用户编号
   * @return 用户的收货信息列表
   */
  @PostMapping("goodsReceiveInfo/getGoodsReceiveInfo")
  public List<GoodsReceiveInfo> getGoodsReceiveInfo(@RequestParam("userId") String userId) {
    return griService.getGoodsReceiveInfo(userId);
  }

  /**
   * 修改收货信息
   *
   * @param grId 收货信息编号
   * @param grName 收货人姓名
   * @param grPhone 收货人电话号码
   * @param grProvinces 收货地址省份
   * @param grCity 收货地址地级市
   * @param grPrefecture 收货地址县级市
   * @param grSpecificAddr 收货地址具体位置
   * @param status 收货地址状态(1为该用户默认收获地址，0则不是)
   * @return 修改成功返回1，否则返回0
   */
  @PostMapping("goodsReceiveInfo/chGoodsReceiveInfo")
  public String chGoodsReceiveInfo(
      @RequestParam("userId") String userId,
      @RequestParam("grId") String grId,
      @RequestParam("grName") String grName,
      @RequestParam("grPhone") String grPhone,
      @RequestParam("grProvinces") String grProvinces,
      @RequestParam("grCity") String grCity,
      @RequestParam("grPrefecture") String grPrefecture,
      @RequestParam("grSpecificAddr") String grSpecificAddr,
      @RequestParam("status") int status) {
    int ret =
        griService.chGoodsReceiveInfo(
            userId,
            grId,
            grName,
            grPhone,
            grProvinces,
            grCity,
            grPrefecture,
            grSpecificAddr,
            status);
    return String.valueOf(ret);
  }

  /**
   * 将收货信息设置为默认（会自动取消其他的默认收货地址）
   *
   * @param userId 用户编号
   * @param grId 收货信息编号
   * @return 设置成功返回1，失败返回0，服务器错误返回500
   */
  @PostMapping("goodsReceiveInfo/setDefault")
  public String setDefault(
      @RequestParam("userId") String userId, @RequestParam("grId") String grId) {
    int ret = griService.setDefault(userId, grId);
    return String.valueOf(ret);
  }

  /**
   * 添加收货地址 参数列表同上
   *
   * @return 新添加收获地址的编号
   */
  @PostMapping("goodsReceiveInfo/addGoodsReceiveInfo")
  public String addGoodsReceiveInfo(
      @RequestParam("userId") String userId,
      @RequestParam("grName") String grName,
      @RequestParam("grPhone") String grPhone,
      @RequestParam("grProvinces") String grProvinces,
      @RequestParam("grCity") String grCity,
      @RequestParam("grPrefecture") String grPrefecture,
      @RequestParam("grSpecificAddr") String grSpecificAddr,
      @RequestParam("status") int status) {
    return griService.addGoodsReceiveInfo(
        userId, grName, grPhone, grProvinces, grCity, grPrefecture, grSpecificAddr, status);
  }

  /**
   * 删除一条收货信息
   *
   * @param grId 收货信息编号
   * @return 删除成功返回1，失败返回0
   */
  @PostMapping("goodsReceiveInfo/delGoodsReceiveInfo")
  public String delGoodsReceiveInfo(@RequestParam("grId") String grId) {
    int ret = griService.delGoodsReceiveInfo(grId);
    return String.valueOf(ret);
  }
}
